package com.wenhao.taptap;

/**
 * @author Wenhao Tong
 * @create 2021-08-28 15:03
 */
public class Main1 {

    public static void main(String[] args) {
        String s = toRelative("/home","/");
        System.out.println(s);
    }
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * <p>
     * 返回该绝对路径（相对于当前路径）的相对路径
     *
     * @param absolutePath string字符串 一个绝对路径
     * @param currentPath  string字符串 一个当前路径
     * @return string字符串
     */
    public static String toRelative(String absolutePath, String currentPath) {
        absolutePath = absolutePath.substring(1);
        currentPath = currentPath.substring(1);
        // write code here
        String[] absolute = absolutePath.split("/");
        String[] current;
        if ("".equals(currentPath)) {
            current = new String[0];
        } else {
            current = currentPath.split("/");
        }
        int index = 0;
        while (index < current.length && index < absolute.length && absolute[index].equals(current[index])) {
            index++;
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0;i < current.length - index;i++) {
            result.append("../");
        }
        for (int i = index;i < absolute.length;i++) {
            result.append(absolute[i] + "/");
        }
        return result.deleteCharAt(result.length() - 1).toString();
    }
}
